<template>
	<view>
		<view class="goods-list">
			<block v-for="(item,index) in goodsList" :key="item.goods_id">
				<my-goods :goods="item" @click="goToDetail"></my-goods>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				params: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				isLoading: false // 节流阀
			};
		},
		onLoad(options) {
			this.params.query = options.query || ''
			this.params.cid = options.cid || ''
			this.getGoodsList()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.goodsList = []
			this.params.pagenum = 1
			this.getGoodsList(uni.StopPullDownRefreshOption)
		},
		// 触底
		onReachBottom() {
			const {
				pagenum,
				pagesize
			} = this.params
			if (pagenum * pagesize >= this.total) return uni.$showMsg('所有数据加载完毕...')
			if (this.isLoading) return
			this.params.pagenum += 1
			this.getGoodsList()

		},
		methods: {
			goToDetail(goods_id) {
				uni.navigateTo({
					url: '/subpkg/goods-detail/goods-detail?goods_id=' + goods_id 
				})
			},
			async getGoodsList(callback) {
				this.isLoading = true
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/search', this.params)
				if (meta.status !== 200) return uni.$showMsg()
				this.isLoading = false
				this.goodsList = [...this.goodsList,...message.goods]
				this.total = message.total
				callback && callback()
			}
		}
	}
</script>

<style lang="scss">

</style>
